using System;
using UnityEngine;
using UnityEngine.UI;

public class GameOver_UI : MonoBehaviour
{
    public CanvasGroup canvasGroup;
    public Sprite[] faceList;
    public Sprite win_title, win_bg, lose_title, lose_bg;
    public Image img_bg, img_face, img_title;
    public Button btn_Restart, btn_Quit, btn_Continue;
    public Animation anim;

    private void Start()
    {
        OpenPage(false);
    }

    private void OnEnable()
    {
        btn_Restart.onClick.AddListener(Restart);
        btn_Quit.onClick.AddListener(BackToMenu);
        btn_Continue.onClick.AddListener(Continue);
    }

    

    private void OnDisable()
    {
        btn_Restart.onClick.RemoveListener(Restart);
        btn_Quit.onClick.RemoveListener(BackToMenu);
        btn_Continue.onClick.RemoveListener(Continue);
    }

    public void GameOver(int arg0)
    {
        img_face.sprite = faceList[arg0];
        if (arg0 == 0)
        {
            img_bg.sprite = lose_bg;
            img_title.sprite = lose_title;
            AudioManager.Instance.PlaySFX(13);
        }
        else
        {
            img_bg.sprite = win_bg;
            img_title.sprite = win_title;
            AudioManager.Instance.PlaySFX(12);
        }
        OpenPage(true);
        if(anim != null)
        {
            anim.Play();
        }
    }

    void OpenPage(bool isOpen)
    {
        Debug.Log("Game Over Page = " + isOpen);
        canvasGroup.alpha = isOpen?1:0;
        canvasGroup.interactable = isOpen;
        canvasGroup.blocksRaycasts = isOpen;
    }


    private void Quit()
    {
        Application.Quit();
    }

    private void BackToMenu()
    {
        GameManager.Instance.BackToMenu();
    }

    private void Restart()
    {
        if (GameManager.Instance != null)
        {
            GameManager.Instance.GameRestart();
        }
    }

    private void Continue()
    {
        if(GameManager.Instance != null)
        {
            GameManager.Instance.NextScene();
        }
    }
}
